<template>
  <div class="fmbox">
    <my-scroll>
      <div class="fm_outter">
        <div class="fm_container">
          <div class="banner">
            <el-carousel
              :interval="4000"
              type="card"
              :height="bannerHeight + 'px'"
            >
              <el-carousel-item
                v-for="(item, index) in banners"
                :key="index"
                style="textAlign:center"
              >
                <img :src="item.imageUrl" />
              </el-carousel-item>
            </el-carousel>
          </div>
          <div class="banner2">
            <el-carousel :interval="5000" height=" 150px">
              <el-carousel-item
                v-for="(item, index) in banners"
                :key="index"
                style="textAlign:center"
              >
                <img :src="item.imageUrl" />
              </el-carousel-item>
            </el-carousel>
          </div>
          <div class="recommended">
            <h3>推荐歌单</h3>
            <div class="recombox">
              <div
                class="recom-item"
                v-for="(item, index) in recommends"
                :key="index"
              >
                <div class="imgbox" @click="golist(item.id)">
                  <div class="dsc">{{ item.copywriter }}</div>
                  <img :src="item.picUrl + '?param=200y200'" alt />
                  <span>
                    <i class="iconfont icon-triangle-down"></i>
                  </span>
                </div>
                <p>{{ item.name }}</p>
              </div>
            </div>
          </div>
          <div class="single">
            <h3>最新单曲</h3>
            <div class="singlebox">
              <div
                class="single-item"
                v-for="(item, index) in singles"
                :key="index"
                @click="onPaly(item)"
              >
                <div class="single-left">
                  <img :src="item.picUrl + '?param=200y200'" alt />
                  <div class="iconbox">
                    <span class="iconfont icon-triangle-down"></span>
                  </div>
                </div>
                <div class="single-right">
                  <p>{{ item.name }}</p>
                  <span>{{ item.song.artists[0].name }}</span>
                </div>
              </div>
            </div>
          </div>
          <div class="mv">
            <h3>推荐MV</h3>
            <div class="mvbox">
              <div
                class="mv-item"
                v-for="(item, index) in mvs"
                :key="index"
                @click="toMv(item.id)"
              >
                <div class="mv-img">
                  <img :src="item.picUrl" alt />
                  <div class="iconbox">
                    <span class="iconfont icon-triangle-down"> </span>
                  </div>
                  <div class="views">
                    <span class="iconfont icon-play"></span>
                    <span>{{ item.playCount }}</span>
                  </div>
                </div>
                <p>{{ item.name }}</p>
                <span>{{ item.artists[0].name }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </my-scroll>
  </div>
</template>
<script>
import { GetBanner, GetRecommend, GetSingle, GetMv } from "../../api/music"
export default {
  name: "findmusic",
  data() {
    return {
      // 图片父容器高度
      bannerHeight: 1000,
      // 浏览器宽度
      screenWidth: 0,
      loading: true,
      banners: [],
      recommends: [],
      singles: [],
      mvs: [],
    }
  },
  created() {
    this.getbanner()
    this.getrecommend()
    this.getsingle()
    this.getmv()
    this.$store.dispatch("getMusic")
  },
  mounted() {
    // 首次加载时,需要调用一次
    this.screenWidth = window.innerWidth
    this.setSize()
    // 窗口大小发生改变时
    window.addEventListener("resize", () => {
      // 变化后需要做的事
      this.screenWidth = window.innerWidth
      this.setSize()
    })
  },
  methods: {
    // 跳转到mv详情页
    toMv(id) {
      this.$router.push(`/details?id=${id}`)
    },
    // 跳转到歌单详情页
    golist(id) {
      this.$router.push(`/musichome/listinfo?id=${id}`)
    },
    // 获取banner
    async getbanner() {
      const result = await GetBanner()
      this.banners = Object.freeze(result.banners)
    },
    // 获取推荐歌单
    async getrecommend() {
      const result = await GetRecommend({
        limit: 10,
      })
      this.recommends = Object.freeze(result.result)
    },
    // 获取单曲
    async getsingle() {
      const result = await GetSingle()
      this.singles = Object.freeze(result.result)
    },
    // 获取mv
    async getmv() {
      const result = await GetMv()
      this.mvs = Object.freeze(result.result)
    },
    // 点击播放
    onPaly(item) {
      this.$store.dispatch("getSong", { id: item.id, autoPlay: 1 })
    },
    setSize: function() {
      // 通过浏览器宽度(图片宽度)计算高度
      if (this.screenWidth > 1200) {
        this.bannerHeight = (500 / 1920) * 0.6 * this.screenWidth
      }
    },
  },
}
</script>
<style lang="scss" scoped>
@import "../../scss/common.scss";
@media (max-width: 600px) {
  .fmbox {
    position: relative;
    width: calc(100% - 0.5rem);
    height: calc(100% - 1rem);
    margin: 0.5rem 0.25rem;
  }
  .fm_container {
    padding-bottom: 2.5rem;
  }
  .banner {
    display: none;
  }
  .banner2 {
    display: block;
    width: calc(100% - 1.25rem);
    margin: 0 0.625rem;
    img {
      width: 100%;
      height: inherit;
    }
  }
  .el-carousel__item {
    border-radius: 10px !important;
    overflow: hidden;
  }
  /deep/ .el-carousel__indicators--horizontal {
    display: none !important;
  }
  .recommended {
    margin: 0rem 0.25rem;
  }
  .recombox {
    display: flex;
    flex-wrap: wrap;
  }
  .recom-item {
    box-sizing: border-box;
    width: 50%;
    padding: 0px 10px;
    margin-top: 20px;
    .imgbox {
      width: 100%;
      position: relative;
      overflow: hidden;
      border-radius: 0.3125rem;
      img {
        width: 100%;
        border-radius: 0.3125rem;
      }
      .dsc {
        position: absolute;
        width: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        top: 0px;
        left: 0px;
        color: rgb(255, 255, 255);
        font-size: 0.875rem;
        line-height: 1.5625rem;
        padding: 0rem 0.125rem;
        box-sizing: border-box;
        transition: 0.5s;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        overflow: hidden;
        box-sizing: border-box;
      }
      span {
        position: absolute;
        right: 10px;
        bottom: 10px;
        width: 35px;
        height: 35px;
        background: rgba(247, 247, 247, 0.5);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        opacity: 1;
      }
      i {
        font-style: normal;
        color: $primary;
        padding-left: 2px;
      }
    }
    p {
      margin: 0px;
      height: 30px;
      line-height: 30px;
      font-size: 13px;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .single {
    margin: 0rem 0.5rem;
    .singlebox {
      .single-item {
        padding: 0rem 0.625rem;
        height: 6.25rem;
        margin-bottom: 1.25rem;
        width: 100%;
        transition: 0.5s;
        display: flex;
        .single-left {
          width: 6.25rem;
          height: 6.25rem;
          overflow: hidden;
          position: relative;
          img {
            width: 6.25rem;
            height: 6.25rem;
          }
          .iconbox {
            position: absolute;
            top: 0px;
            left: 0px;
            height: 100%;
            width: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: 0.5s;
            span {
              color: $primary;
              width: 35px;
              height: 35px;
              background: rgba(247, 247, 247, 0.5);
              display: flex;
              justify-content: center;
              align-items: center;
              border-radius: 50%;
            }
            span:before {
              transform: translateX(1px);
            }
          }
        }
        .single-right {
          flex: auto;
          margin-left: 10px;
          p {
            word-wrap: break-word;
            word-break: normal;
          }
          span {
            word-wrap: break-word;
            word-break: normal;
            margin-top: 15px;
            display: inline-block;
            color: rgb(128, 125, 124);
            font-size: 13px;
          }
        }
      }
    }
  }
  .mv {
    margin: 0rem 0.25rem;
    .mvbox {
      .mv-item {
        width: 100%;
        box-sizing: border-box;
        padding: 0.5rem;
        margin-bottom: 30px;
        .mv-img {
          width: 100%;
          position: relative;
          img {
            width: 350px;
          }
          .iconbox {
            position: absolute;
            top: 0px;
            left: 0px;
            height: 100%;
            width: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: 0.5s;
            span {
              color: $primary;
              width: 60px;
              height: 60px;
              background: rgba(247, 247, 247, 0.5);
              display: flex;
              justify-content: center;
              align-items: center;
              border-radius: 50%;
            }
            span:before {
              font-size: 30px;
              transform: translateX(2px);
            }
          }
          .views {
            position: absolute;
            top: 10px;
            right: 10px;
            span {
              margin-left: 10px;
              color: white;
            }
          }
        }
        .mv-img:hover {
          cursor: pointer;
        }
        .mv-img:hover .iconbox {
          opacity: 1;
        }
      }
      p {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin: 10px 0px;
      }
      span {
        font-size: 14px;
        color: rgb(143, 140, 140);
      }
    }
  }
}
@media (min-width: 600px) {
  h3 {
    height: 40px;
    line-height: 40px;
    background: rgb(236, 236, 236);
    padding-left: 10px;
  }
  .fmbox {
    position: relative;
    width: 100%;
    height: 100%;
  }
  .fm_outter {
    width: calc(100vw - 230px) !important;
    min-width: 970px;
    height: 100%;
    display: flex;
    justify-content: center;
  }
  .fm_container {
    width: 100%;
    max-width: 1200px;
  }
  .banner {
    display: block;
    width: calc(100% - 40px);
    margin: 20px;
    img {
      width: 100%;
      height: inherit;
    }
  }
  .banner2 {
    display: none;
  }
  .el-carousel__item {
    border-radius: 10px !important;
    overflow: hidden;
  }
  .recommended {
    margin: 0px 20px;
  }
  .recombox {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .recom-item {
    box-sizing: border-box;
    width: 20%;
    padding: 0px 10px;
    margin-top: 20px;
    .imgbox {
      width: 100%;
      position: relative;
      overflow: hidden;
      img {
        border-radius: 5px;
        width: 100%;
        object-fit: cover;
      }
      .dsc {
        position: absolute;
        width: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        top: -50px;
        left: 0px;
        color: rgb(255, 255, 255);
        font-size: 14px;
        line-height: 25px;
        padding: 0px 5px;
        transition: 0.5s;
        box-sizing: border-box;
      }
      span {
        position: absolute;
        right: 10px;
        bottom: 10px;
        width: 35px;
        height: 35px;
        background: rgba(247, 247, 247, 0.5);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        opacity: 1;
        transition: 0.5s;
      }
      i {
        font-style: normal;
        color: $primary;
        padding-left: 2px;
      }
    }
    .imgbox:hover .dsc {
      top: 0px;
    }
    .imgbox:hover span {
      opacity: 1;
    }
    .imgbox:hover {
      cursor: pointer;
    }
    p {
      margin: 0px;
      height: 30px;
      line-height: 30px;
      font-size: 13px;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .single {
    margin: 0px 20px;
    .singlebox {
      margin: 0px 10px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      .single-item {
        height: 100px;
        margin-bottom: 20px;
        width: 49%;
        transition: 0.5s;
        display: flex;
        .single-left {
          width: 100px;
          height: 100px;
          overflow: hidden;
          position: relative;
          img {
            width: 100px;
          }
          .iconbox {
            position: absolute;
            top: 0px;
            left: 0px;
            height: 100%;
            width: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: 0.5s;
            span {
              color: $primary;
              width: 35px;
              height: 35px;
              background: rgba(247, 247, 247, 0.5);
              display: flex;
              justify-content: center;
              align-items: center;
              border-radius: 50%;
            }
            span:before {
              transform: translateX(1px);
            }
          }
        }
        .single-right {
          flex: auto;
          margin-left: 10px;
          p {
            word-wrap: break-word;
            word-break: normal;
          }
          span {
            margin-top: 15px;
            display: inline-block;
            color: rgb(128, 125, 124);
            font-size: 13px;
          }
        }
      }
      .single-item:hover {
        cursor: pointer;
        background: rgba(221, 220, 219, 0.8);
      }
      .single-item:hover .iconbox {
        opacity: 1;
      }
    }
  }
  .mv {
    margin: 0px 20px;
    .mvbox {
      display: flex;
      overflow: hidden;
      .mv-item {
        width: 25%;
        padding: 10px;
        box-sizing: border-box;
        margin-bottom: 30px;
        .mv-img {
          width: 100%;
          position: relative;
          img {
            width: 100%;
            object-fit: cover;
          }
          .iconbox {
            position: absolute;
            top: 0px;
            left: 0px;
            height: 100%;
            width: 100%;
            background: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: 0.5s;
            span {
              color: $primary;
              width: 60px;
              height: 60px;
              background: rgba(247, 247, 247, 0.5);
              display: flex;
              justify-content: center;
              align-items: center;
              border-radius: 50%;
            }
            span:before {
              font-size: 30px;
              transform: translateX(2px);
            }
          }
          .views {
            position: absolute;
            top: 10px;
            right: 10px;
            span {
              margin-left: 10px;
              color: white;
            }
          }
        }
        .mv-img:hover {
          cursor: pointer;
        }
        .mv-img:hover .iconbox {
          opacity: 1;
        }
      }
      p {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin: 10px 0px;
      }
      span {
        font-size: 14px;
        color: rgb(143, 140, 140);
      }
    }
  }
}
</style>
